import 'package:flutter/material.dart';
import 'package:interview/theme/app_colors.dart';
import 'package:interview/utils/format.dart';

class RecordInfo extends StatefulWidget {
  final AppColors theme;
  final int recordingDuration;
  final List<double> decibelsHistory;

  RecordInfo({
    super.key,
    required this.theme,
    required this.recordingDuration,
    required this.decibelsHistory,
  });

  @override
  _RecordInfoState createState() => _RecordInfoState();
}

class _RecordInfoState extends State<RecordInfo> {
  final ScrollController _scrollController = ScrollController(); // 滚动控制器

  // 将分贝值分段映射为对应的高度
  double _mapDecibelsToHeight(double value) {
    if (value > 120) {
      return 50.0; // 最大分贝限制高度为 50
    } else if (value > 80 && value <= 120) {
      return 30 + (value - 40) * 0.5; // 中等偏高分贝映射到高度 30-50
    } else if (value > 30 && value <= 80) {
      return 20 + (value - 30) * 1.0; // 中等分贝映射到高度 20-30
    } else if (value > 0 && value <= 30) {
      return value * 0.66; // 小分贝映射到高度 0-20
    } else {
      return 2.0; // 防止过小分贝不可见，设置最低高度为 5
    }
  }

  @override
  void didUpdateWidget(covariant RecordInfo oldWidget) {
    super.didUpdateWidget(oldWidget);

    // 当 `decibelsHistory` 更新时，自动滚动到末尾
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (_scrollController.hasClients) {
        _scrollController.animateTo(
          _scrollController.position.maxScrollExtent,
          duration: Duration(milliseconds: 300),
          curve: Curves.easeOut, // 滚动时的缓动效果
        );
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    // 将分贝数据映射为高度
    final processedHeights = widget.decibelsHistory.map(_mapDecibelsToHeight).toList();

    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16 , vertical: 24),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Padding(
            padding: const EdgeInsets.only(top: 12,bottom: 24),
            child: Text(Format.formatDuration(widget.recordingDuration) , style: TextStyle(fontSize: 12 , fontWeight: FontWeight.w500 , color:widget.theme.contrastMix3))
          ),
          SizedBox(
            height: 50, // 可视化区域高度
            child: ListView.builder(
              controller: _scrollController, // 绑定滚动控制器
              scrollDirection: Axis.horizontal,
              itemCount: processedHeights.length,
              itemBuilder: (context, index) {
                return Center(
                  child: AnimatedContainer(
                    duration: Duration(milliseconds: 400), // 动画时长
                    curve: Curves.easeInOut, // 平滑动画曲线
                    margin: EdgeInsets.symmetric(horizontal: 1.2), // 条之间的间距
                    width: 2.0, // 每个条的宽度
                    height: processedHeights[index], // 映射后的动态高度
                    decoration: BoxDecoration(
                      color: Colors.redAccent, // 条的颜色
                      borderRadius: BorderRadius.circular(1), // 圆角条形
                    ),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    _scrollController.dispose(); // 释放控制器资源
    super.dispose();
  }
}
